package com.manuelvs.bobbysadventure;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import com.apperhand.device.android.AndroidSDKProvider;

import android.content.Intent;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class splash extends BaseGameActivity implements IUpdateHandler
{
	private Camera mCamera;

    private BitmapTextureAtlas mBitmapTextureAtlas;
    
    private TiledTextureRegion textureJugar;
    
    private TextureRegion Fondo;
    
    SpriteBackground bg;
    
    AnimatedSprite siguiente;
    
    char alpha=0;
    
    private Scene mMainScene;

	@Override
	public Engine onLoadEngine() 
	{
		AndroidSDKProvider.initSDK(this);
		mCamera = new Camera(0, 0, 480, 320);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(480, 320), mCamera));
	}

	@Override
	public void onLoadResources() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        this.mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR);
        this.Fondo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "splash.png", 0, 0);

		if(getText(R.string.language).toString().equals("es"))
		{
	        this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/siguiente.png", 0, 321, 1, 2);
		}
		if(getText(R.string.language).toString().equals("pt"))
		{
	        this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/siguiente.png", 0, 321, 1, 2);
		}
		if(getText(R.string.language).toString().equals("other"))
		{
	        this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "next.png", 0, 321, 1, 2);
		}
        this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		
	    mMainScene = new Scene();
	    
		bg = new SpriteBackground(new Sprite(0, 0, Fondo));
		mMainScene.setBackground(bg);
		
		siguiente = new AnimatedSprite(330, 288, textureJugar)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN)
            	{
            		siguiente.animate(new long[] {30}, new int[] {1}, 30);
            		siguiente.setScale(1.25f);
	            	
            	}
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
            	{
            		final Intent Main = new Intent(splash.this, Main.class);
            		siguiente.animate(new long[] {30}, new int[] {0}, 30);
            		siguiente.setScale(1f);
	            	startActivity(Main);
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(siguiente);
	    
	    
	    mMainScene.registerTouchArea(siguiente);

	    mMainScene.setTouchAreaBindingEnabled(true);
	    
		mMainScene.registerUpdateHandler(this);
		
	    return mMainScene;
	}

	@Override
	public void onLoadComplete() {}

	@Override
	public void onUpdate(float pSecondsElapsed) 
	{
		alpha++;
		if(alpha==100)
		{
    		final Intent Main = new Intent(splash.this, Main.class);
    		startActivity(Main);
		}
	}

	@Override
	public void reset() {}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.more_apps:
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=ManuelVS"));
        	startActivity(browserIntent);
            return true;
        case R.id.configuration:
        	final Intent configuration = new Intent(splash.this, configuration.class);
        	startActivity(configuration);
            return true;
        }
        return false;
        
    }
}
